<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CMLAdmin extends CI_Controller {
 	public function __construct()
    {
        parent::__construct();
        session_start();
        //$this->output->enable_profiler(FALSE);
    	if (!isset($_SESSION['username'])){
	    	redirect('administration','refresh');
	    }
        elseif($_SESSION['username']=='dara')
        {
            redirect('itstock','refresh');
        }
	    $this->load->model('MSetting');
        $this->load->model('MStock');
    }
	    
	public function index()
	{
		$data['title'] = "Dashboard - Stock Inventory System";
		$data['main'] = 'dashboard';
        $info = $this->MStock->getStockInfo();
        $data['instock'] = 0;
        $data['used'] = 0;
        $data['broken'] = 0;
        if(is_array($info) && count($info) ) {
            foreach ($info as $item)
            {
                if($item->status_id == 1)
                    $data['instock'] = $item->cnt;
                elseif($item->status_id == 2)
                    $data['used'] = $item->cnt;
                elseif($item->status_id ==3)
                    $data['broken'] = $item->cnt;
            }
        }
        
		$this->load->vars($data);
		$this->load->view('layout'); 
	}
    
    public function accessDenied()
    {
        $data['title'] = 'Access Denied';
		$data['main'] = 'accessdenied';     
		$this->load->vars($data);
		$this->load->view('layout');  
    }
/*--contract--*/
    public function contractList()
    {
        $data['title'] = "Manage Contract Type";
		$data['main'] = 'setting/contractlist';
        $data['items'] = $this->MSetting->getContractList();
		$this->load->vars($data);
		$this->load->view('layout');    
    }
    
    public function contractAdd()
    {
        if($this->input->post('contracttype'))
        {
            $this->MSetting->addEditContract();
	  		$this->session->set_flashdata('message','New Type added');
	  		redirect('cmladmin/contractlist','refresh');
        }
        $data['title'] = "Add Contract Type";
		$data['main'] = 'setting/contractadd';
        $data['form'] = $this->MFormGlobal->getButtonsAdd('cmladmin/contractlist');
		$this->load->vars($data);
		$this->load->view('layout');    
    }
    
    public function contractEdit($id=0)
    {
        if($this->input->post('contracttype'))
        {
            $this->MSetting->addEditContract(id_clean($this->input->post('contracttype_id')));
	  		$this->session->set_flashdata('message','Contract Type edited');
	  		redirect('cmladmin/contractlist','refresh');
        }
        $data['title'] = "Update Contract Type";
		$data['main'] = 'setting/contractedit';
        $data['contract'] = $this->MSetting->getContractById($id);
        $data['form'] = $this->MFormGlobal->getButtonsEdit('cmladmin/contractlist','cmladmin/deletecontract/'.$id);
		$this->load->vars($data);
		$this->load->view('layout');    
    }
    
    public function deleteContract($id=0)
    {
        $this->MSetting->deleteContract($id);
       	$this->session->set_flashdata('message','Contract Type deleted');
  		redirect('cmladmin/contractlist','refresh');    
    }
/*--end---*/
    
/*--print type--*/
    public function printList()
    {
        $data['title'] = "Manage Printing Type";
		$data['main'] = 'setting/printlist';
        $data['items'] = $this->MSetting->getTypeList();
		$this->load->vars($data);
		$this->load->view('layout');    
    }
    
    public function printAdd()
    {
        if($this->input->post('type'))
        {
            $this->MSetting->addEditType();
	  		$this->session->set_flashdata('message','New Type added');
	  		redirect('cmladmin/printlist','refresh');
        }
        $data['title'] = "Add Printing Type";
		$data['main'] = 'setting/printadd';
        $data['form'] = $this->MFormGlobal->getButtonsAdd('cmladmin/printlist');
		$this->load->vars($data);
		$this->load->view('layout');    
    }
    
    public function printEdit($id=0)
    {
        if($id <= 2 )
        {
       	    $this->session->set_flashdata('message','It is default type, you can not make any change to it.');
	  		redirect('cmladmin/printlist','refresh');
        }
        
        if($this->input->post('type'))
        {
            $this->MSetting->addEditType(id_clean($this->input->post('type_id')));
	  		$this->session->set_flashdata('message','Print edited');
	  		redirect('cmladmin/printlist','refresh');
        }
        $data['title'] = "Update Printing Type";
		$data['main'] = 'setting/printedit';
        $data['print'] = $this->MSetting->getPrintById($id);
        $data['form'] = $this->MFormGlobal->getButtonsEdit('cmladmin/printlist','cmladmin/deleteprint/'.$id);
		$this->load->vars($data);
		$this->load->view('layout');    
    }
    
    public function deletePrint($id=0)
    {
        $this->MSetting->deleteType($id);
       	$this->session->set_flashdata('message','Print deleted');
  		redirect('cmladmin/printlist','refresh');    
    }
/*---end---*/

/*--Location--*/
    public function locationList()
    {
        $data['title'] = "Manage Printing Type";
		$data['main'] = 'setting/locationlist';
        $data['items'] = $this->MSetting->getLocationList();
		$this->load->vars($data);
		$this->load->view('layout');    
    }
    
    public function locationAdd()
    {
        if($this->input->post('location'))
        {
            $this->MSetting->addEditLocation();
	  		$this->session->set_flashdata('message','New Location added');
	  		redirect('cmladmin/locationlist','refresh');
        }
        $data['title'] = "Add New Location";
		$data['main'] = 'setting/locationadd';
        $data['form'] = $this->MFormGlobal->getButtonsAdd('cmladmin/locationlist');
		$this->load->vars($data);
		$this->load->view('layout');    
    }
    
    public function locationEdit($id=0)
    {
        if($this->input->post('location'))
        {
            $this->MSetting->addEditLocation(id_clean($this->input->post('location_id')));
	  		$this->session->set_flashdata('message','Location edited');
	  		redirect('cmladmin/locationlist','refresh');
        }
        $data['title'] = "Update location";
		$data['main'] = 'setting/locationedit';
        $data['location'] = $this->MSetting->getLocationById($id);
        $data['form'] = $this->MFormGlobal->getButtonsEdit('cmladmin/locationlist','cmladmin/deletelocation/'.$id);
		$this->load->vars($data);
		$this->load->view('layout');    
    }
    
    public function deleteLocation($id=0)
    {
        $this->MSetting->deleteLocation($id);
       	$this->session->set_flashdata('message','Location deleted');
  		redirect('cmladmin/locationlist','refresh');    
    }
/*---end---*/

/*-----Assets---------*/
    public function assetList($cat)
    {
        if($cat != 3 && $cat != 4)
        {
            redirect('/','refresh');    
        }
        if($cat==3)
            $title = "Manage FA Type";
        else
            $title = "Manage NA Type";
            
        $data['title'] = $title;
		$data['main'] = 'setting/assetlist';
        $data['cat'] = $cat;
        $data['items'] = $this->MSetting->getAssetList($cat);
		$this->load->vars($data);
		$this->load->view('layout');    
    }
    
    public function assetAdd($cat=0)
    {
        if($cat==0)
        {
            $cat = $this->input->post('cat_id');
        }
        if($cat != 3 && $cat != 4)
        {
            redirect('/','refresh');    
        }
        if($cat==3)
            $title = "Add New FA Type";
        else
            $title = "Add New NA Type";
        
            
        if($this->input->post('type'))
        {
            $this->MSetting->addEditAsset($cat);
	  		$this->session->set_flashdata('message','New type added');
	  		redirect('cmladmin/assetlist/'.$cat,'refresh');
        }
        $data['cat'] = $cat;
        $data['title'] = $title;
		$data['main'] = 'setting/assetadd';
        $data['form'] = $this->MFormGlobal->getButtonsAdd('cmladmin/assetlist/'.$cat);
		$this->load->vars($data);
		$this->load->view('layout');    
    }
    
    public function assetEdit($cat=0, $id=0)
    {
        if($cat==0)
        {
            $cat = $this->input->post('cat_id');
        }
        if($cat != 3 && $cat != 4)
        {
            redirect('/','refresh');    
        }
        if($cat==3)
            $title = "Update FA Type";
        else
            $title = "Update NA Type";
        
        
        if($this->input->post('type'))
        {
            $this->MSetting->addEditAsset($cat, id_clean($this->input->post('id')));
	  		$this->session->set_flashdata('message','Item edited');
	  		redirect('cmladmin/assetlist/'.$cat,'refresh');
        }
        $data['cat'] = $cat;
        $data['title'] = $title;
		$data['main'] = 'setting/assetedit';
        $data['asset'] = $this->MSetting->getAssetById($cat, $id);
        $data['form'] = $this->MFormGlobal->getButtonsEdit('cmladmin/assetlist/'.$cat,'cmladmin/deleteasset/'.$cat.'/'.$id);
		$this->load->vars($data);
		$this->load->view('layout');    
    }
    
    public function deleteAsset($cat, $id=0)
    {
        if($cat != 3 && $cat != 4)
        {
            redirect('/','refresh');    
        }
        
        $this->MSetting->deleteAsset($cat, $id);
       	$this->session->set_flashdata('message','Item deleted');
  		redirect('cmladmin/assetlist/'.$cat,'refresh');    
    }
/*-----end------------*/
    
    public function help()
    {
        $data['title'] = "Chamroeun Stock Inventory System";
		$data['main'] = 'setting/help';
		$this->load->vars($data);
		$this->load->view('layout');    
    }

}
